using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class X3AndBonusInMenu : MonoBehaviour
{
	public static X3AndBonusInMenu instance;

	public GameObject x3Event;

	public GameObject bonusEvent;

	private IEnumerator _timer;

	private bool _canShow;

	private bool _marathonUnAvailable;

	private bool _x3Updated;

	public float pauseUnixTime;

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		EventX3Manager.EventX3Updated += OnEventsUpdated;
		ChestBonusController.OnChestBonusChange += OnEventsUpdated;
		MarathonController.OnRewardUnavailable += OnRewardUnavailableEventHandler;
		_timer = Timer();
		StartCoroutine(_timer);
		StartCoroutine(WaiteEvents());
	}

	public void OnRewardUnavailableEventHandler()
	{
		_marathonUnAvailable = true;
	}

	public void OnEventsUpdated()
	{
		_x3Updated = true;
	}

	private IEnumerator WaiteEvents()
	{
		while (!PromoActionManager.jsonReaded || !_x3Updated || !_marathonUnAvailable)
		{
			yield return null;
		}
		if (!(EventX3Manager.Instance != null))
		{
			yield break;
		}
		if (EventX3Manager.Instance.promoList.Count == 0)
		{
			if (PromoActionManager.ReplacePereliv.enabled)
			{
				BCWDebug.LogWarning("Add Pereliv", "x3AndBonus");
				EventX3Manager.Instance.promoList.Add(EventX3Manager.Instance.ShowPromo);
			}
			if (((bool)EventX3Manager.Instance && EventX3Manager.Instance.IsEventX3Active) || ((bool)ChestBonusController.instance && ChestBonusController.instance.IsBonusActive))
			{
				BCWDebug.LogWarning("Add x3", "x3AndBonus");
				EventX3Manager.Instance.promoList.Add(EventX3Manager.Instance.ShowX3);
			}
			if (PromoActionManager.NewUser(PromoActionManager.fyber.newUserStatusHours))
			{
				BCWDebug.LogWarning("Add RateUs", "x3AndBonus");
				EventX3Manager.Instance.promoList.Add(EventX3Manager.Instance.ShowRateUs);
			}
		}
		if (EventX3Manager.Instance.promoList.Count > 0)
		{
			int tmp = Random.Range(0, EventX3Manager.Instance.promoList.Count);
			EventX3Manager.Instance.promoList[tmp]();
			EventX3Manager.Instance.promoList.RemoveAt(tmp);
		}
	}

	private void OnDestroy()
	{
		instance = null;
		EventX3Manager.EventX3Updated -= OnEventsUpdated;
		ChestBonusController.OnChestBonusChange -= OnEventsUpdated;
		MarathonController.OnRewardUnavailable -= OnRewardUnavailableEventHandler;
	}

	public IEnumerator ShowPromo()
	{
		yield return new WaitForSeconds(PromoActionManager.fyber.secondsToShowX3);
		while (MarketControllerUI.instance.MarketActive() || controllerMenu.thisScript.blockPanel.gameObject.activeSelf)
		{
			yield return null;
		}
		ReplacePerelivController.instance.ShowPromo();
	}

	public IEnumerator ShowX3()
	{
		while (MarketControllerUI.instance.MarketActive() || controllerMenu.thisScript.blockPanel.gameObject.activeSelf || !controllerMenu.thisScript.panelMainMenu.gameObject.activeSelf || !_canShow)
		{
			yield return new WaitForEndOfFrame();
		}
		if (EventX3Manager.Instance != null)
		{
			if (EventX3Manager.Instance.IsEventX3Active)
			{
				x3Event.SetActive(true);
				bonusEvent.SetActive(false);
				FlurryWrapper.LogEvent(parameters: new Dictionary<string, string> { { "x3_Banner_Transition", "x3BannerShow" } }, eventName: FlurryWrapper.EV_X3_TRANSATION);
			}
			else if (ChestBonusController.instance != null && ChestBonusController.instance.IsBonusActive)
			{
				x3Event.SetActive(false);
				bonusEvent.SetActive(true);
			}
			else
			{
				bonusEvent.SetActive(false);
				x3Event.SetActive(false);
				BCWDebug.Log("CheckMissionCompleteWindow");
			}
		}
	}

	public void CanShowMessage()
	{
		if (_timer != null)
		{
			StopCoroutine(_timer);
		}
		_canShow = true;
	}

	public void Close()
	{
		x3Event.SetActive(false);
		bonusEvent.SetActive(false);
	}

	public void OpenShop()
	{
		x3Event.SetActive(false);
		bonusEvent.SetActive(false);
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("x3_Banner_Transition", "x3BannerTransition");
		FlurryWrapper.LogEvent(FlurryWrapper.EV_X3_TRANSATION, dictionary);
		StartCoroutine(MarketControllerUI.instance.ShowBank());
	}

	private void OnApplicationPause(bool pauseStatus)
	{
		if (pauseStatus)
		{
			pauseUnixTime = EventX3Manager.CurrentUnixTime;
		}
		else if ((float)EventX3Manager.CurrentUnixTime - pauseUnixTime > (float)PromoActionManager.fyber.secondsAfterPauseToShowX3)
		{
			CanShowMessage();
			OnEventsUpdated();
		}
	}

	private IEnumerator Timer()
	{
		while (!PromoActionManager.jsonReaded)
		{
			yield return null;
		}
		yield return new WaitForSeconds(PromoActionManager.fyber.secondsToShowX3);
		_canShow = true;
		_timer = null;
	}
}
